//
//  ContentView.swift
//  DingDIngAuto
//
//  Created by liguandong on 2024/3/11.
//

import SwiftUI

struct ContentView: View {
    @StateObject var viewModel = MainViewModel()
    
    let editWidth = UIScreen.main.bounds.width * 0.10
    var wakeUpDateLable : String{
        get {
            let date = viewModel.nextWakeUpDate // 假设为当前日期和时间
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MM月dd日 HH:mm"
            return dateFormatter.string(from: date)
        }
    }
    var body: some View {
        ZStack {
            HStack() {
                Text("下次触发时间 : ")
                Text("\(wakeUpDateLable)")
                    .font(.title3)
                    .foregroundColor(Color.red)
                    .animation(.easeInOut,value: 1)
            }
            .padding(.top,30)
            .frame(maxWidth: .infinity,maxHeight: .infinity,alignment: .topLeading)
            
            Button("收起键盘") {
                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
            }
            .padding(.top,70)
            .frame(maxWidth: .infinity,maxHeight: .infinity,alignment: .topLeading)
            
            // 添加一个手势来收起键盘
//           Spacer()
//               .onTapGesture {
//                   debugPrint("点击收起键盘")
//                   UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
//               }
//               .background(Color.blue.opacity(0.5))
            
            VStack() {
                Button("跳转钉钉，务必点击一次") {
                    if openDingTalkUrl(){
                        let alertController = UIAlertController(title: nil, message: "打开钉钉失败", preferredStyle: .alert)
                    }
                }.padding()
                .frame(minHeight: 50)
                .background(RoundedRectangle(cornerRadius: 20).fill(Color.red.opacity(0.4)))
                
                Spacer().frame(height: 20)
                HStack {
                    Text("跳过周六日")
//                    Toggle(isOn: $viewModel.isSkipWeekdays) {
//
//                    }
                    Toggle("", isOn: $viewModel.isSkipWeekdays)
                               .frame(width: 50)
                                
                }
                
                HStack {
                    Text("上班时间:")
                    TextField("Enter a number", value: $viewModel.atWorkHour, formatter: NumberFormatter())
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .frame(width: editWidth)
                    Text("h")
                    TextField("Enter a number", value: $viewModel.atWorkMin, formatter: NumberFormatter())
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .frame(width: editWidth)
                    Text("m")
                }.padding()
                HStack {
                    Text("下班时间:")
                    TextField("Enter a number", value: $viewModel.offWorkHour, formatter: NumberFormatter())
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .frame(width: editWidth)
                    Text("h")
                    TextField("Enter a number", value: $viewModel.offWorkMin, formatter: NumberFormatter())
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .frame(width: editWidth)
                    Text("m")
                }.padding()
                HStack {
                    Text("上班时间随机时间:")
                    TextField("Enter a number", value: $viewModel.atWorkRandomMin, formatter: NumberFormatter())
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .frame(width: editWidth)
                    Text("m")
                }.padding()
                HStack {
                    Text("下班时间随机时间:")
                    TextField("Enter a number", value: $viewModel.offWorkRandomMin, formatter: NumberFormatter())
                                    .textFieldStyle(RoundedBorderTextFieldStyle())
                                    .frame(width: editWidth)
                    Text("m")
                }.padding()
                
                
                Button {
                    viewModel.saveData()
                } label: {
                    Text("保存更新")
                        .padding()
                        .frame(minWidth: 100,minHeight: 50)
                        .background(RoundedRectangle(cornerRadius: 20).fill(Color.red.opacity(0.4)))
                    
                    
                }
                .frame(maxWidth: .infinity,alignment: .center)
            } .onTapGesture {
                debugPrint("tap2")
                      UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                  }
        }
        .onTapGesture {
            debugPrint("tap1")
                  UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
              }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
